#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @Time		:2018/3/5 10:39
# @Author	:RNone
import json
from json.decoder import JSONDecodeError
import logging
from api.login import login


class common_modify(login):
	def get_login_session(self, caseDict):
		'''获取登陆后的requests对象'''
		session = self.do_login(caseDict)  # 获取登陆后的session
		return session

	def test_modify_data(self, caseDict):
		'''数据查询测试'''
		session = self.get_login_session(caseDict)
		'''获取页面verify'''
		host = 'http://' + caseDict['host']
		r = session.get(url=host + '/DataCenters')
		verify = self.find_verify(r.text)
		'''执行查询vpn信息'''
		modifyData = caseDict['params']  # 查询参数
		modifyData['__RequestVerificationToken'] = verify
		modifyData = self.list_to_tup(modifyData)
		logging.debug('发送修改测试数据')
		r = session.post(url=host + '/Common/PostModifly', data=modifyData)
		try:
			data = json.loads(r.text)
		except JSONDecodeError as e:
			self.write_error(e)
			data = r.text
		caseDict['response'] = data
		result = self.modify_result(r.text)
		if caseDict['expect'] == result:
			caseDict['result'] = 'PASS'
		else:
			caseDict['result'] = 'FAIL'
		return caseDict

	def modify_result(self, content):
		'''数据查询结果校验'''
		logging.debug('校验修改数据结果')
		if '增加数据成功' in content:
			return 'succ'
		elif '删除数据成功' in content:
			return 'succ'
		elif '更新数据成功' in content:
			return 'succ'
		else:
			return 'fail'


if __name__ == '__main__':
	params = {'host': '192.168.60.186'}
	csa = common_modify()
	csa.test_modify_data(params)
